package br.com.paiva.container.util;

import java.util.List;

import javax.faces.model.DataModel;

public class PagedDataModel extends DataModel{

private int rowIndex = -1;
	
	private int totalNumRows;
  
	private int pageSize;
  
	@SuppressWarnings("rawtypes")
	private List list;
  
	public PagedDataModel() {
		super();
	}
  
	@SuppressWarnings("rawtypes")
	public PagedDataModel(List list, int totalNumRows) {
	    super();
	    setWrappedData(list);
	    this.totalNumRows = totalNumRows;
	    this.pageSize = list.size();
	}
  
	public boolean isRowAvailable() {
	    if(list == null)
	      return false;
    
	    int rowIndex = getRowIndex();
	    if(rowIndex >=0 && rowIndex < list.size())
	      return true;
	    else
	    return false;
	}

	public int getRowCount() {
		return totalNumRows;
	}

	public Object getRowData() {
	    if(list == null)
	      return null;
	    else if(!isRowAvailable())
	      throw new IllegalArgumentException();
	    else {
	      int dataIndex = getRowIndex();
	      return list.get(dataIndex);
	    }
	}

	public int getRowIndex() {
		return (rowIndex % pageSize);	
	}

	public void setRowIndex(int rowIndex) {
    this.rowIndex = rowIndex;
  }

	public Object getWrappedData() {
		return list;
	}
  
	@SuppressWarnings("rawtypes")
	public void setWrappedData(Object list) {
		this.list = (List) list;
	}
	
}
